Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing python's global and nonlocal #1735

Merged
merged 19 commits into from
Oct 8, 2024
Merged

Implementing python's global and nonlocal #1735

merged 19 commits into from
Oct 8, 2024

Conversation

oxisto
Copy link
Member

@oxisto oxisto commented Sep 26, 2024

This PR tries to implement python's global keyword and tries to replicate python variable resolution as good as possible.

I had to add a ReferenceScopeModifierStatement node, which is able to solve both global and nonlocal, but I am not 100 % happy with the solution. Another approach would be to store this scope modification in the scope itself, so this PR would need some discussion.

nonlocal cannot be tested until we actually support parsing nested functions #1736

@oxisto oxisto added the python label Sep 26, 2024
@oxisto oxisto marked this pull request as ready for review September 26, 2024 23:19
This was linked to issues Sep 26, 2024
@oxisto oxisto changed the title Implementing python's global Implementing python's global and nonlocal Sep 27, 2024
Copy link
Contributor

@maximiliankaul maximiliankaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd appreciate to have two PRs:

  • one for the CPG changes (with it's own tests, so that we can see how it's supposed to work in general and not only in the specific python use-case)
  • one making use of the first PR by implementing the Python stuff

I get why you modify the scope of a Reference as this is how the Python front-end works. However, don't we need a scope modifier for the declaration (I get that this will be problematic with the Python "add declarations pass"?

@oxisto
Copy link
Member Author

oxisto commented Sep 27, 2024

I'd appreciate to have two PRs:

  • one for the CPG changes (with it's own tests, so that we can see how it's supposed to work in general and not only in the specific python use-case)
  • one making use of the first PR by implementing the Python stuff

Done. #1742

I get why you modify the scope of a Reference as this is how the Python front-end works. However, don't we need a scope modifier for the declaration (I get that this will be problematic with the Python "add declarations pass"?

The declaration's scope is not modified. Only the "lookup scope" of the symbol that you are trying to resolve in the current scope is modified (e.g., from the local to the global scope).

The trick is that the add-declaration-pass will then create the variable in the lookup-scope (if it does not exist). So if we have a global c and a c = 2 in the block, there will be a global lookup and if it fails, we will create the node in the global scope. I will improve this logic in this PR a little bit after #1742 is merged in.

This PR adds a new node type `LookupScopeStatement`, which can be used to adjust the lookup scope of symbols that are resolved in the current scope. Most prominent examples are Python's `global` and `nonlocal` statements.

Support for python will be added in a later PR. This will only add the node and provides the basic functionality in the lookup.
This PR tries to implement python's `global` keyword and tries to replicate python variable resolution as good as possible.
Copy link

sonarcloud bot commented Sep 27, 2024

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 89.09091% with 6 lines in your changes missing coverage. Please review.

Project coverage is 74.94%. Comparing base (3e2db5c) to head (e33fa16).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ofer/aisec/cpg/passes/PythonAddDeclarationsPass.kt 90.00% 1 Missing and 3 partials ⚠️
...fer/aisec/cpg/frontends/python/StatementHandler.kt 86.66% 0 Missing and 2 partials ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
...fer/aisec/cpg/frontends/python/StatementHandler.kt 79.89% <86.66%> (+0.97%) ⬆️
...ofer/aisec/cpg/passes/PythonAddDeclarationsPass.kt 90.16% <90.00%> (+11.59%) ⬆️

... and 4 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@oxisto oxisto mentioned this pull request Oct 4, 2024
@oxisto oxisto enabled auto-merge (squash) October 8, 2024 09:14
@oxisto oxisto merged commit 1b4602d into main Oct 8, 2024
2 checks passed
@oxisto oxisto deleted the python-global branch October 8, 2024 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle nonlocal keyword Handle global keyword
2 participants